#include "AdaptiveLogrithmicReproduction.h"

AdaptiveLogrithmicReproduction::AdaptiveLogrithmicReproduction(double _Ldmax, double _Lwmax, double _bias, int _logBase): ToneReproductionModel(_Ldmax, _Lwmax), bias(_bias), logBase(_logBase){
}

double AdaptiveLogrithmicReproduction::getBias(double t){
	double exponent = log(bias)/log(0.5);
	return pow(t, exponent);
}

void AdaptiveLogrithmicReproduction::calculateLd(){
	double logNLwmax = log(Lwmax + 1)/log(logBase);
	double LwLogBase = 2.0+getBias(Lw/Lwmax)*8.0;
	double logMLw = log(Lw + 1)/log(LwLogBase);
	//Ld = ((Ldmax*0.01)/logNLwmax)*(logMLw);
	Ld = (logMLw/logNLwmax);
}

void AdaptiveLogrithmicReproduction::adjustScene(vector<MyColor>* pixels){
	calculateLw(pixels);
	calculateLd();

	for(size_t i = 0; i < pixels->size(); i++){
		MyColor* iColor = &(pixels->at(i));
		if(iColor->r < 0){
			iColor->r = 0;
		}
		else if(iColor->r > 1){
			iColor->r = 1;
		}
		if(iColor->g < 0){
			iColor->g = 0;
		}
		else if(iColor->g > 1){
			iColor->g = 1;
		}
		if(iColor->b < 0){
			iColor->b = 0;
		}
		else if(iColor->b > 1){
			iColor->b = 1;
		}
     
		iColor->r *= Ld;
		iColor->g *= Ld;
		iColor->b *= Ld;
	}
}